package me.xplabs.utils 
{
	/**
	 * 简单的对象池，在需要时可创建对象池
	 * ...
	 * @author xiaohan
	 */
	public class SimpleObjPool 
	{
		private var MAX_VALUE:uint; 
		private var GROWTH_VALUE:uint; 
		private var counter:uint; 
		private var pool:Array; 
		private var cla:Class;
		/**
		 * @param	pcla 需要被创建的Class对象
		 * @param	maxPoolSize 初始创建的对象个数
		 * @param	growthValue 对象池对象不够时每次创建的个数
		 */
		public function SimpleObjPool(pcla:Class,maxPoolSize:uint, growthValue:uint) 
		{
			MAX_VALUE = maxPoolSize; 
			GROWTH_VALUE = growthValue; 
			counter = maxPoolSize; 
			 
			var i:uint = maxPoolSize;
			cla = pcla;
			
			pool = new Array(MAX_VALUE); 
			while( --i > -1 ) 
				pool[i] = new cla(); 
		}
		/**
		 * 获取对象池的对象
		 * @return
		 */
		public function getObj():* 
		{ 
			if ( counter > 0 ) 
				return pool[--counter];
				 
			var i:uint = GROWTH_VALUE; 
			while( --i > 0 ) 
					pool.unshift(new cla());
			counter = GROWTH_VALUE - 1; 
			return new cla(); 
			 
		}
		/**
		 * 销毁对象并放入对象池中
		 * @param	disposedObj
		 */
		public function dispose(disposedObj:*):void 
		{
			pool[counter++] = disposedObj; 
		} 
	}

}